/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmDepartmentManager.java
 *
 * Created on 12-03-2011, 23:58:39
 */

package GUI;

import interact.DataInteraction;
import interact.ValidateData;
import interact.connect;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author UnderGround
 */
public class frmDepartmentManager extends javax.swing.JPanel {
    Vector vt;
    /** Creates new form frmDepartmentManager */
    public frmDepartmentManager() {
        initComponents();
        connect.loadDataTable3("getDepartment", tblDepartment);
    }
    public static void loadTable(){
        connect.loadDataTable3("getDepartment", tblDepartment);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Panel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblDepartment = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int vColIndex) {
                return false;
            }
        };
        jPanel1 = new javax.swing.JPanel();
        txtDepartment = new javax.swing.JTextField();
        btnDel = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        Panel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tblDepartment.setAutoCreateRowSorter(true);
        tblDepartment.setBackground(new java.awt.Color(255, 255, 204));
        tblDepartment.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Department ID", "Department name"
            }
        ));
        tblDepartment.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tblDepartment);

        Panel.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 720, 370));

        jPanel1.setBackground(new java.awt.Color(153, 153, 153));

        btnDel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/del.png"))); // NOI18N
        btnDel.setText("Delete");
        btnDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelActionPerformed(evt);
            }
        });

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/up.png"))); // NOI18N
        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/add_depart.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setText("Department Name");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(137, 137, 137)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(btnDel))
                    .addComponent(txtDepartment, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(236, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDepartment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnUpdate)
                    .addComponent(btnDel))
                .addContainerGap())
        );

        Panel.add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 370, 720, 130));

        add(Panel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 720, 500));
    }// </editor-fold>//GEN-END:initComponents

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        vt = new Vector();
        int rowSelected = tblDepartment.getSelectedRow();
         String departmentName = txtDepartment.getText();
        if(rowSelected == -1){
            ValidateData.showMessage("please choose department to update");
            return;
        }
        else if(!ValidateData.checkContent(departmentName,txtDepartment)){
            ValidateData.showMessage("Department invalid! plz enter again ");
            return;
        }else if(!ValidateData.checkDuplicateData(departmentName, "checkDuplicate_department", "count")){
            ValidateData.showMessage("duplicate department");
            return;
        }
        else{
        String id =(String) tblDepartment.getValueAt(rowSelected, 0);
        vt.add(id);
        vt.add(departmentName);
        DataInteraction.updateData("updateDepartment", vt);
        loadTable();
          ValidateData.showMessage("update department successful");
        }
}//GEN-LAST:event_btnUpdateActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        vt = new Vector();
       
        String department = txtDepartment.getText().trim();
         System.out.println(ValidateData.checkDuplicateData(department, "checkDuplicate_department", "count"));
         if(!ValidateData.checkContent(department,txtDepartment)){
            ValidateData.showMessage("Department invalid! plz enter again ");
            return;
        }else if(!ValidateData.checkDuplicateData(department, "checkDuplicate_department", "count")){
            ValidateData.showMessage("duplicate department");
            return;
        }
        else{
        vt.add(department);
        DataInteraction.updateData("addDepartment", vt);
        loadTable();
        ValidateData.showMessage("add Department successful");
        }
}//GEN-LAST:event_btnAddActionPerformed

    private void btnDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelActionPerformed
        vt = new Vector();
        int rowSelected = tblDepartment.getSelectedRow();
      
        if (rowSelected == -1) {
            ValidateData.showMessage("please choose department to delete");
            return;
        }
           String id = (String) tblDepartment.getValueAt(rowSelected, 0);
         if(ValidateData.check_Status_To_Del("check_del_department",id )){
            ValidateData.showMessage("You can't delete this department");
            return;
        }else {
            vt.add(id);
            DataInteraction.deleteRecord("delDepartment", id);
            loadTable();
            ValidateData.showMessage("delete successful");
        }
}//GEN-LAST:event_btnDelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private static javax.swing.JPanel Panel;
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDel;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private static javax.swing.JTable tblDepartment;
    private javax.swing.JTextField txtDepartment;
    // End of variables declaration//GEN-END:variables

}
